package com.wmh.baseservice.operationaid.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.operationaid.entity.ProductTrafficSourceOptions;
import com.wmh.baseservice.operationaid.mapper.ProductTrafficSourceOptionsMapper;
import com.wmh.baseservice.operationaid.pojo.vo.productTrafficSourceOptions.ProductTrafficSourceOptionQueryReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.productTrafficSourceOptions.ProductTrafficSourceOptionsCreateReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.productTrafficSourceOptions.ProductTrafficSourceOptionsUpdateReqVO;
import com.wmh.baseservice.operationaid.service.ProductTrafficSourceOptionsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 谢骏
 * @description 针对表【operation_aid_product_trafficsource_options(运营助手-商品数据-流量来源-选项)】的数据库操作Service实现
 * @createDate 2023-07-21 16:33:53
 */
@Service
public class ProductTrafficsourceOptionsServiceImpl extends ServiceImpl<ProductTrafficSourceOptionsMapper, ProductTrafficSourceOptions>
        implements ProductTrafficSourceOptionsService {

    @Override
    public void create(ProductTrafficSourceOptionsCreateReqVO reqVO) {
        checkNameUniqueness(null, reqVO.getName());
        ProductTrafficSourceOptions entity = new ProductTrafficSourceOptions()
                .setName(reqVO.getName())
                .setParentId(reqVO.getParentId());
        baseMapper.insert(entity);
    }

    @Override
    public void update(ProductTrafficSourceOptionsUpdateReqVO reqVO) {
        checkExist(reqVO.getId());
        checkNameUniqueness(reqVO.getId(), reqVO.getName());
        ProductTrafficSourceOptions entity = new ProductTrafficSourceOptions()
                .setId(reqVO.getId()).setName(reqVO.getName()).setParentId(reqVO.getParentId());
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        checkExist(id);
        baseMapper.delete(new LambdaQueryWrapperX<ProductTrafficSourceOptions>()
                .eq(ProductTrafficSourceOptions::getId,id));
        baseMapper.delete(new LambdaQueryWrapperX<ProductTrafficSourceOptions>()
                .eq(ProductTrafficSourceOptions::getParentId,id));
    }

    @Override
    public List<ProductTrafficSourceOptions> getListNotBaseField(ProductTrafficSourceOptionQueryReqVO reqVO) {
        return baseMapper.selectListNotBaseField(reqVO);
    }

    private void checkNameUniqueness(Long id, String name) {
        ProductTrafficSourceOptions entity = baseMapper.selectOne(new LambdaQueryWrapperX<ProductTrafficSourceOptions>()
                .neIfPresent(ProductTrafficSourceOptions::getId, id)
                .eq(ProductTrafficSourceOptions::getName, name));
        if (entity != null) {
            throw new ServiceException("存在相同名称的选项");
        }
    }

    private void checkExist(Long id){
        ProductTrafficSourceOptions entity = baseMapper.selectById(id);
        if (entity == null) {
            throw new ServiceException("该选项不存在");
        }
    }
}




